package cn.jiahua.hospital.controller;

import cn.jiahua.hospital.constants.ResultEnum;
import cn.jiahua.hospital.dto.Page;
import cn.jiahua.hospital.dto.Result;
import cn.jiahua.hospital.pojo.Patients;
import cn.jiahua.hospital.pojo.User;
import cn.jiahua.hospital.service.PatientsService;
import cn.jiahua.hospital.utils.RedisUtil;
import cn.jiahua.hospital.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api
@RequestMapping("/api/patients")
@RestController
public class PatientsController {

    @Resource
    PatientsService patientsService;


    @Resource
    RedisUtil redisUtil ;



    @GetMapping("getInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "当前页码", required = false),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = false),
    })
    @ApiOperation(value = "获取就诊人信息", notes = "获取用户常用就诊人信息")
    public Result getInfoByPage(@RequestParam(value = "pageNo", required = false) Integer pageNo,
                                @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return ResultUtils.returnFail(ResultEnum.FAIL_PARAM.getCode(), ResultEnum.FAIL_PARAM.getMsg());
        }
        User user = (User) redisUtil.get(token);
        Integer userId = user.getId();
        pageNo = pageNo == null ? 0 : pageNo;
        pageSize = pageSize == null ? 0 : pageSize;
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        Integer patientsCountByMap = patientsService.getPatientsCountByMap(map);
        Page page = new Page(pageNo, pageSize, patientsCountByMap);
        Page<Patients> patientsPage = patientsService.queryPatientsPageByMap(map, page.getBeginPos(), page.getPageSize());
        List<Patients> list = patientsPage.getList();
        if (list == null || list.size() <= 0) {
            return ResultUtils.returnFail(ResultEnum.FAIL_HAVE_NOT_EXIST.getCode(), ResultEnum.FAIL_HAVE_NOT_EXIST.getMsg());
        }
        return ResultUtils.returnDataSuccess(patientsPage);
    }


    @GetMapping("getTokenPatientInfo")
    @ApiOperation(value = "通过token获取就诊人信息", notes = "获取用户常用就诊人信息")
    public Result getTokenPatientInfo(HttpServletRequest request) {
        String token =  request.getHeader("token");
        System.out.println("getTokenPatientInfo--------------------------------"+token);
        User user = (User)redisUtil.get(token);
        System.out.println(user.getId());
        Integer userId = null;
       if(user!=null){
           userId= user.getId();
       }
        if (userId == null) {
            return ResultUtils.returnFail(ResultEnum.FAIL_PARAM.getCode(), ResultEnum.FAIL_PARAM.getMsg());
        }

        HashMap map = new HashMap();
        map.put("id",userId);
        List<Patients> patients =  patientsService.getPatientsListByMap(map);
        if (patients == null) {
            return ResultUtils.returnFail(ResultEnum.FAIL_HAVE_NOT_EXIST.getCode(), ResultEnum.FAIL_HAVE_NOT_EXIST.getMsg());
        }
        return ResultUtils.returnDataSuccess(patients);
    }



    @ApiOperation(value = "添加就诊人信息", notes = "用户个人中心和预约订单时，添加就诊人信息")
    @PostMapping("add")
    public Result addPatients(@RequestBody Patients patients,HttpServletRequest request) {

        String token = request.getHeader("token");
        if (token == null) {
            return ResultUtils.returnFail(ResultEnum.FAIL_PARAM.getCode(), ResultEnum.FAIL_PARAM.getMsg());
        }
        User user = (User) redisUtil.get(token);
        Integer userId = user.getId();
        patients.setUserId(userId);
        Integer integer = patientsService.qdtxAddPatients(patients);
        if (integer <= 0) {
            return ResultUtils.returnFail(ResultEnum.FAIL_USER_UPDATE.getCode(), ResultEnum.FAIL_USER_UPDATE.getMsg());
        }
        return ResultUtils.returnSuccess();

    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, value = "用户id"),
            @ApiImplicitParam(name = "patientsId", required = true, value = "就诊人id")
    })
    @ApiOperation(value = "删除就诊人信息", notes = "用户个人中心和预约订单时，删除就诊人信息")
    @PostMapping("del")
    public Result delPatients(Integer userId, Integer patientsId) {
        if (userId == null || patientsId == null) {
            return ResultUtils.returnFail(ResultEnum.FAIL_PARAM.getCode(), ResultEnum.FAIL_PARAM.getMsg());
        }
        //根据条件获取就诊人信息
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("patientsId", patientsId);
        List<Patients> patientsListByMap = patientsService.getPatientsListByMap(map);
        if (patientsListByMap == null || patientsListByMap.size() <= 0) {
            return ResultUtils.returnFail(ResultEnum.FAIL_HAVE_NOT_EXIST.getCode(), ResultEnum.FAIL_HAVE_NOT_EXIST.getMsg());
        }
        Patients patients = patientsListByMap.get(0);
        Integer id = patients.getId();
        //根据id删除就诊人信息
        Integer integer = patientsService.qdtxDeletePatientsById(id);
        if (integer <= 0) {
            return ResultUtils.returnFail(ResultEnum.FAIL_USER_UPDATE.getCode(), ResultEnum.FAIL_USER_UPDATE.getMsg());
        }
        return ResultUtils.returnSuccess();

    }


    @ApiOperation(value = "根据就诊人Id获取就诊人信息", notes = "获取就诊人信息")
    @GetMapping("getpatient")
    public Result getPatient(Integer patientId) {
        HashMap map = new HashMap();
        map.put("id",patientId);
        ArrayList<Patients> list = (ArrayList<Patients>) patientsService.getPatientsListByMap(map);
        if(list!=null){
            return ResultUtils.returnDataSuccess(list.get(0));
        }
        return ResultUtils.returnFail();

    }
}
